﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using QuickTasks.ViewModels.Data;
using Microsoft.Advertising.Mobile.UI;

namespace QuickTasks.Views.Message
{
    public partial class SendSms : PhoneApplicationPage
    {
       
        RetriveData retriveNo = new RetriveData();
        QuickMessages objQuickMessages = new QuickMessages();
        List<QuickMessageProp> objQuickMessageList = new List<QuickMessageProp>();
        string Qid = string.Empty;
        
        public SendSms()
        {
            InitializeComponent();
        
            Loaded += new RoutedEventHandler(SendSms_Loaded);
        }
    
        void SendSms_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (App.Subject != "") tbxTitle.Text = App.Subject;
                if (App.Message != "") tbxMobilemessage.Text = App.Message;
                string Qtype = string.Empty;

                if (NavigationContext.QueryString.TryGetValue("quickId", out Qid))
                {

                    if (NavigationContext.QueryString.TryGetValue("quicktype", out Qtype))
                    {
                        List<QuickMessageProp> objmessage = new List<QuickMessageProp>();
                        using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                        {
                            var favpeoplelist = from i in context.message where i.id.ToString() == Qid select i;
                            foreach (var favpeople in favpeoplelist)
                            {
                                tbxTitle.Text = favpeople.subject;
                                App.Nmobile = favpeople.reciepient;
                                App.Mobile = retriveNo.mnos(App.Nmobile);
                                tbxMobilemessage.Text = favpeople.message;
                                break;
                            }
                        }
                    }
                }
                IEnumerable<System.Windows.Navigation.JournalEntry> back = NavigationService.BackStack;
                foreach (var b in back)
                {
                    if (b.Source.ToString().Contains("SendSms.xaml"))
                        NavigationService.RemoveBackEntry();
                    break;
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in  SendSms_Loaded event In SendSms.xaml page", ex);
            }
        }

        private void ThemeableImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            App.Subject = tbxTitle.Text;
            App.Message = tbxMobilemessage.Text;
            NavigationService.Navigate(new Uri("/Views/FavoritePeople/FavoriteMobile.xaml?nav=1", UriKind.Relative));
        }

       

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbxTitle.Text == "")
                {
                    tblkTitleValidation.Visibility = Visibility.Visible;
                    return;
                }
                else
                    tblkTitleValidation.Visibility = Visibility.Collapsed;

                if (App.Mobile == "")
                {
                    tblkRecipientmobileValidation.Visibility = Visibility.Visible;
                    return;
                }
                else
                    tblkRecipientmobileValidation.Visibility = Visibility.Collapsed;

                if (tbxMobilemessage.Text == "")
                {
                    tblkMessagemobileValidation.Visibility = Visibility.Visible;
                    return;
                }
                else
                    tblkMessagemobileValidation.Visibility = Visibility.Collapsed;


                objQuickMessages.SendMessage("", App.Mobile, tbxTitle.Text, tbxMobilemessage.Text, "SMS");
                App.Subject = "";
                App.Message = "";
                NavigationService.Navigate(new Uri("/MainPage.xaml?sectionId=1", UriKind.Relative));
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in  ApplicationBarIconButton_Click event In SendSms.xaml page", ex);
            }
        }
    }
}
